package com.ruoyi.web.controller.participator;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.ParticipantInformation;
import com.ruoyi.service.IParticipantInformationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * 参与者信息Controller
 *
 * @author 杨琛
 * @date 2020-10-16
 */
@Controller
@RequestMapping("/ruoyi/information")
public class ParticipantInformationController extends BaseController {
    private String prefix = "ruoyi/information";

    /**
     * 点击添加参与者和查看参与者接受的id
     */
    private Long userId;


    @Autowired
    private IParticipantInformationService participantInformationService;

    @RequiresPermissions("ruoyi:information:view")
    @GetMapping()
    public String information() {
        return prefix + "/information";
    }

    /**
     * 查询参与者信息列表
     */
    @RequiresPermissions("ruoyi:information:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ParticipantInformation participantInformation) {
        startPage();
        //只显示和附件有关的参与者
        participantInformation.setAccessoryId(userId);
        List<ParticipantInformation> list = participantInformationService.selectParticipantInformationList(participantInformation);
        return getDataTable(list);
    }

    /**
     * 导出参与者信息列表
     */
    @RequiresPermissions("ruoyi:information:export")
    @Log(title = "参与者信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ParticipantInformation participantInformation) {
        List<ParticipantInformation> list = participantInformationService.selectParticipantInformationList(participantInformation);
        ExcelUtil<ParticipantInformation> util = new ExcelUtil<ParticipantInformation>(ParticipantInformation.class);
        return util.exportExcel(list, "information");
    }

    /**
     * 新增参与者信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存参与者信息
     */
    @RequiresPermissions("ruoyi:information:add")
    @Log(title = "参与者信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ParticipantInformation participantInformation) {
        //将上传人的附件id传过来赋值给其他参与人的id
        participantInformation.setAccessoryId(this.userId);
        return toAjax(participantInformationService.insertParticipantInformation(participantInformation));
    }

    /**
     * 修改参与者信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ParticipantInformation participantInformation = participantInformationService.selectParticipantInformationById(id);
        mmap.put("participantInformation", participantInformation);
        return prefix + "/edit";
    }

    /**
     * 修改保存参与者信息
     */
    @RequiresPermissions("ruoyi:information:edit")
    @Log(title = "参与者信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ParticipantInformation participantInformation) {
        return toAjax(participantInformationService.updateParticipantInformation(participantInformation));
    }

    /**
     * 删除参与者信息
     */
    @RequiresPermissions("ruoyi:information:remove")
    @Log(title = "参与者信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(participantInformationService.deleteParticipantInformationByIds(ids));
    }

    /**
     * 打开参与者信息页面
     * @return 结果
     */
    @RequiresPermissions("ruoyi:participant:view")
    @GetMapping("/participant/{userId}")
    public String participant(@PathVariable("userId") Long userId) {
        //获取附件id
        this.userId = userId;
        return prefix + "/information";
    }

    /**
     * 查看参与者信息
     * @return 结果
     */
    @RequiresPermissions("ruoyi:selectInformation:view")
    @GetMapping("/selectInformation/{userId}")
    public String selectInformation(@PathVariable("userId") Long userId) {
        this.userId = userId;
        return prefix + "/selectinformations";
    }
}
